package com.ooono.factorytest.items;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.ooono.factorytest.R;
import com.ooono.factorytest.base.BaseActivity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class SimTestActivity extends BaseActivity {

    private static final String TAG = "SimTestActivity";

    public static final String KEY = "sim";

    private TextView sim1ValueTv;
    private TextView sim2ValueTv;

    private SubscriptionManager mSubscriptionManager;
    private List<SubscriptionInfo> mSubInfoList = null;

    @Override
    public int getTestTitle() {
        return R.string.sim_test_prompt;
    }

    @Override
    public String getTestKey() {
        return KEY;
    }

    @Override
    public int getContentLayout() {
        return R.layout.content_sim_test;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mSubscriptionManager = SubscriptionManager.from(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter simIntentFilter = new IntentFilter();
        simIntentFilter.addAction("android.intent.action.SIM_STATE_CHANGED");
        registerReceiver(mSimStateIntentReceiver, simIntentFilter);

        simDetecting();
    }

    @Override
    public void initViews() {
        sim1ValueTv = findViewById(R.id.text_view_sim1_value);
        sim2ValueTv = findViewById(R.id.text_view_sim2_value);
    }

    /*
     * SIM detecting
     */
    private void simDetecting(){
        mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();

        final TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        final int numSlots = tm.getPhoneCount();
        int numSim = 0;

        Log.d(TAG, "simDetecting numSim:" + numSim + " numSlots:" + numSlots);

        if (numSlots == 0) {
            sim1ValueTv.setText(R.string.sim_test_uninstall);
            sim2ValueTv.setText(R.string.sim_test_uninstall);
        }

        if (numSlots == 1) {
            boolean hasIcc = tm.hasIccCard();

            if (!hasIcc){
                sim1ValueTv.setText(getResources().getString(R.string.sim_test_uninstall) + getResources().getString(R.string.fail));
            }else{
                numSim++;
                sim1ValueTv.setText(getResources().getString(R.string.sim_test_install) + getResources().getString(R.string.success));
            }
        }

        Class tmclass = tm.getClass();
        Method method_hasIccCard = null;
        try {
            method_hasIccCard = tmclass.getMethod("hasIccCard", int.class);
        } catch(NoSuchMethodException | SecurityException e) {
        }
        if (numSlots >= 2) {
            for (int i = 0; i < numSlots; ++i) {
                boolean hasIcc = false;
                if(method_hasIccCard!=null) {
                    try {
                        hasIcc=(Boolean)method_hasIccCard.invoke(tm, i);
                    } catch(IllegalAccessException | IllegalArgumentException |
                            InvocationTargetException e) {
                    }
                }
                if (i == 0){
                    Log.d(TAG, "simDetecting - 1");
                    if (!hasIcc) {
                        Log.d(TAG, "simDetecting - 2");
                        sim1ValueTv.setText(getResources().getString(R.string.sim_test_uninstall) + getResources().getString(R.string.fail));
                    }else{
                        Log.d(TAG, "simDetecting - 3");
                        numSim++;
                        sim1ValueTv.setText(getResources().getString(R.string.sim_test_install) + getResources().getString(R.string.success));
                    }
                }else if (i == 1){
                    if (!hasIcc) {
                        Log.d(TAG, "simDetecting - 4");
                        sim2ValueTv.setText(getResources().getString(R.string.sim_test_uninstall) + getResources().getString(R.string.fail));
                    }else{
                        Log.d(TAG, "simDetecting - 5");
                        numSim++;
                        sim2ValueTv.setText(getResources().getString(R.string.sim_test_install) + getResources().getString(R.string.success));
                    }
                }
            }
        }
        Log.d(TAG, "simDetecting - 6 numSim:" + numSim + " numSlots:" + numSlots);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mSimStateIntentReceiver);
        super.onDestroy();
    }

    private BroadcastReceiver mSimStateIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("android.intent.action.SIM_STATE_CHANGED")) {
                simDetecting();
            }
        }
    };

}